﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Library;
using System.Data;

namespace OA.Exam
{
    public partial class SetGroups : System.Web.UI.Page
    {
        Exam myExam = new Exam();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //生成树形结构
                myExam.GetGroupTree(TreeView1);
                //TreeView1.Attributes.Add("onclick", "javascript:return GoSelect();");
                
                //绑定题型
                BindTypes(DdlTypeUp);//绑定题型
                BindTypes(DdlTypeDown);//绑定题型
            }
        }

        protected void BindTypes(DropDownList myList)
        {
            myList.DataSource = Exam.GetQuestionTypes();
            myList.DataBind();
        }
        //选择节点分组
        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (TreeView1.SelectedNode.Value == "all")
            {
                return;
            }
            string groupId = TreeView1.SelectedValue;
            HiddenGroupId.Value = groupId;
            BindHadSet(groupId, "-1", "");
            BindCanSet(groupId, "-1", "");
        }
        //绑定已加入分组的题目列表
        protected void BindHadSet(string groupId, string typeId, string subject)
        {
            string sql = string.Empty;
            sql = "select a.QuestionId,b.Subject,c.TypeName from Exam_QuestionsGroups as a left join Exam_Questions as b on a.QuestionId=b.QuestionId left join Exam_QuestionTypes as c on b.ItemType=c.TypeId where GroupId='" + groupId + "'";
            if (typeId != "-1")
            {
                sql += " and b.ItemType='" + typeId + "'";
            }
            if (subject != "")
            {
                sql += " and subject like '%" + subject + "%'";
            }
            DataTable dt = DataAccess.getDataTable(sql);
            GridViewQuestionsUp.DataSource = dt.DefaultView;
            GridViewQuestionsUp.DataBind();
        }
        //上方查询已加入题目
        protected void BtnSearcnUp_Click(object sender, EventArgs e)
        {
            string typeId = "-1";
            string subject = string.Empty;
            if (DdlTypeUp.SelectedValue != "-1")
            {
                typeId = DdlTypeUp.SelectedValue;
            }
            if (TxtSubjectUp.Text.Trim() != "")
            {
                subject = TxtSubjectUp.Text.Trim();
            }
            BindHadSet(HiddenGroupId.Value, typeId, subject);
        }
        //绑定可选的题目列表
        protected void BindCanSet(string groupId, string typeId, string subject)
        {
            string sql = string.Empty;
            sql = "select QuestionId,Subject,TypeName from Exam_Questions as a left join Exam_QuestionTypes as b on a.ItemType=b.TypeId where QuestionId not in (select QuestionId from Exam_QuestionsGroups where GroupId='" + groupId + "')";
            if (typeId != "-1")
            {
                sql += " and ItemType='" + typeId + "'";
            }
            if (subject != "")
            {
                sql += " and subject like '%" + subject + "%'";
            }
            DataTable dt = DataAccess.getDataTable(sql);
            GridViewQuestionsDown.DataSource = dt.DefaultView;
            GridViewQuestionsDown.DataBind();
        }
        //下方查询可以设置分组的题目
        protected void BtnSearchDown_Click(object sender, EventArgs e)
        {
            string typeId = "-1";
            string subject = string.Empty;
            if (DdlTypeDown.SelectedValue != "-1")
            {
                typeId = DdlTypeDown.SelectedValue;
            }
            if (TxtSubjectDown.Text.Trim() != "")
            {
                subject = TxtSubjectDown.Text.Trim();
            }
            BindCanSet(HiddenGroupId.Value, typeId, subject);
        }
        //批量将题目加入分组
        protected void BtnSet_Click(object sender, EventArgs e)
        {
            int mark = 0;
            string questionId = string.Empty;
            bool flag = false;
            for (int i = 0; i < GridViewQuestionsDown.Rows.Count; i++)
            {
                CheckBox ChbCheck = (CheckBox)GridViewQuestionsDown.Rows[i].FindControl("ChbCheck");
                questionId = GridViewQuestionsDown.DataKeys[i].Value.ToString();
                if (ChbCheck.Checked)
                {
                    mark++;
                    flag = myExam.InsertGroup(questionId, HiddenGroupId.Value);
                }
            }
            if (mark == 0)
            {
                LabPrompt.Text = "您没有选择题目";
                return;
            }
            if (flag)
            {
                LabPrompt.Text = "设置成功";
                BindHadSet(HiddenGroupId.Value, DdlTypeUp.SelectedValue, TxtSubjectUp.Text.Trim());
                BindCanSet(HiddenGroupId.Value, DdlTypeDown.SelectedValue, TxtSubjectDown.Text.Trim());
            }
        }
        //批量去除已设置的分组
        protected void BtnRemove_Click(object sender, EventArgs e)
        {
            int mark = 0;
            string questionId = string.Empty;
            bool flag = false;
            for (int i = 0; i < GridViewQuestionsUp.Rows.Count; i++)
            {
                CheckBox ChbCheck = (CheckBox)GridViewQuestionsUp.Rows[i].FindControl("ChbCheck");
                questionId = GridViewQuestionsUp.DataKeys[i].Value.ToString();
                if (ChbCheck.Checked)
                {
                    mark++;
                    flag = myExam.DeleteGroup(questionId, HiddenGroupId.Value);
                }
            }
            if (mark == 0)
            {
                LabPrompt.Text = "您没有选择题目";
                return;
            }
            if (flag)
            {
                LabPrompt.Text = "移除分组成功";
                BindHadSet(HiddenGroupId.Value, DdlTypeUp.SelectedValue, TxtSubjectUp.Text.Trim());
                BindCanSet(HiddenGroupId.Value, DdlTypeDown.SelectedValue, TxtSubjectDown.Text.Trim());
            }
        }
    }
}
